package com.changfeng.service.impl;

import com.changfeng.bean.ReatailersBrand;
import com.changfeng.bean.ReatailersBrandCategoryMerge;
import com.changfeng.bean.dto.vo.BrandListVO;
import com.changfeng.bean.dto.vo.BrandVO;
import com.changfeng.bean.dto.vo.CategoryListVO;
import com.changfeng.dao.ReatailersBrandCategoryMergeMapper;
import com.changfeng.dao.ReatailersBrandMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.BrandService;
import com.changfeng.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private ReatailersBrandMapper reatailersBrandMapper;

    @Autowired
    private ReatailersBrandCategoryMergeMapper reatailersBrandCategoryMergeMapper;

    @Autowired
    private CategoryService categoryService;
    @Override
    public BrandVO findBrandById(Integer brandId) {
        ReatailersBrand reatailersBrand = reatailersBrandMapper.selectByPrimaryKey(brandId);

        if (reatailersBrand == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        List<CategoryListVO> byBrandId = categoryService.findByBrandId(brandId);

        BrandVO vo = new BrandVO();
        vo.setBrandId(brandId);
        vo.setBrandName(reatailersBrand.getBrandName());
        vo.setBrandState(reatailersBrand.getBrandState());
        vo.setCategoryList(byBrandId);
        return vo;
    }

    @Override
    public List<BrandListVO> findListAll() {
        return reatailersBrandMapper.findAll();
    }

    @Override
    public void addCategory(Integer brandId, Integer categoryId) {
        ReatailersBrandCategoryMerge merge = new ReatailersBrandCategoryMerge();
        merge.setBrandId(brandId);
        merge.setCategoryId(categoryId);
        merge.setGmtCreate(new Date());
        merge.setGmtModified(new Date());
        reatailersBrandCategoryMergeMapper.insert(merge);
    }

    @Override
    public void deleteCategory(Integer brandId, Integer categoryId) {

        reatailersBrandCategoryMergeMapper.deleteByBrandAndCategory(brandId,categoryId);

    }

    @Override
    public void saveBrand(BrandListVO vo) {
        ReatailersBrand brand = new ReatailersBrand();
        brand.setBrandName(vo.getBrandName());
        brand.setBrandState(vo.getBrandState());
        brand.setGmtCreate(new Date());
        brand.setGmtModified(new Date());

        reatailersBrandMapper.insert(brand);
    }

    @Override
    public void deleteBrand(Integer brandId) {
        reatailersBrandMapper.deleteByPrimaryKey(brandId);
    }

    @Override
    public void updateBrand(BrandListVO vo) {
        ReatailersBrand brand = reatailersBrandMapper.selectByPrimaryKey(vo.getBrandId());
        if (brand == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        brand.setGmtModified(new Date());
        brand.setBrandName(vo.getBrandName());
        brand.setBrandState(vo.getBrandState());
        reatailersBrandMapper.updateByPrimaryKey(brand);
    }
}
